/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.upload;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import queenb.net.entitybean.N100;
import queenb.web.controller.QueenbController;
import queenb.web.crm.controller.CustomerController;
import queenb.web.crm.store.CustomerStore;
import queenb.web.util.ContextUtil;

/**
 *
 * @author HP
 */
public class DisplayImageServlet extends HttpServlet {

    private static final int DEFAULT_BUFFER_SIZE = 10240;
    private final String PATH_IMAGE = "images/avatar2.jpg";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        // CustomerController cusBean = (CustomerController)ContextUtil.lookupBeanFromExternalServlet("CustomerBean", request, response);
        String pathDefaultPhoto = request.getRealPath(PATH_IMAGE);
        String imgId = request.getParameter("pn100Id");
        int pn100 = 0;
       
        CustomerStore customer = new CustomerStore();

        if (imgId != null) {
            pn100 = Integer.parseInt(imgId.toString());

        }



        try {
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            List<N100> list = customer.getListN100NC144(pn100, "luantt@queenb.vn");
            if (list != null) {
                if (list.get(0).getNc144() != null) {
                    buffer = list.get(0).getNc144();
                } else {
                    File file = new File(pathDefaultPhoto);
                    FileInputStream fis = new FileInputStream(file);
                    BufferedImage image = ImageIO.read(fis);
                    if (image != null) {
                        ByteArrayOutputStream bas = new ByteArrayOutputStream();
                        ImageIO.write(image, "jpg", bas);

                        buffer = bas.toByteArray();
                         bas.close();
                    }

                   
                }
            }
            response.setContentType("image/jpg");
            response.getOutputStream().write(buffer, 0, buffer.length);
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (Exception ex) {
            System.out.println("--image errr0 ---" + ex.getMessage());
        }
       
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
